package controller

import (
	jsoniter "github.com/json-iterator/go"
	"github.com/kataras/iris/v12"
	"github.com/kataras/iris/v12/mvc"
	"gowork/autoPurchase/model"
	"strconv"
)

type AutoPurchaseController struct {
	Ctx iris.Context
	RawMaterial *model.RawMaterial
}

func (ac *AutoPurchaseController)PostInsertautopurchaserawmaterialsupplier() mvc.Result {

	ac.RawMaterial = new(model.RawMaterial)

	aprsListStr := ac.Ctx.FormValue("aprsList")
	var aprsListu []*model.AutoPurchaseRawMaterialSupplier
	jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal([]byte(aprsListStr), &aprsListu)

	aprsListb, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(&aprsListu)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	rawMaterialIdStr := ac.Ctx.FormValue("rawMaterialId")
	rawMaterialId, err := strconv.ParseInt(rawMaterialIdStr, 10, 64)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	rawMaterialName := ac.Ctx.FormValue("rawMaterialName")

	res, err := ac.RawMaterial.InsertAutoPurchaseRawMaterialSupplier(string(aprsListb), rawMaterialId, rawMaterialName)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	return mvc.Response{
		Object : map[string]interface{}{"message": res["message"], "data": nil, "code":res["code"],},
	}
}

func (ac *AutoPurchaseController)PostGenerateautomaticpurchasedemand() mvc.Result {

	ac.RawMaterial = new(model.RawMaterial)

	res, err := ac.RawMaterial.GenerateAutomaticPurchaseDemand()
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	return mvc.Response{
		Object : map[string]interface{}{"message": "成功", "data": res, "code":"200",},
	}
}

func (ac *AutoPurchaseController)PostGenerateautomaticpurchasedemandasync() mvc.Result {

	ac.RawMaterial = new(model.RawMaterial)

	res, err := ac.RawMaterial.GenerateAutomaticPurchaseDemandAsync()
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	return mvc.Response{
		Object : map[string]interface{}{"message": "成功", "data": res, "code":"200",},
	}
}

func (ac *AutoPurchaseController)PostQueryrawmaterialinfo() mvc.Result {

	ac.RawMaterial = new(model.RawMaterial)

	rawMaterialIdStr := ac.Ctx.FormValue("rawMaterialId")
	rawMaterialId, err := strconv.ParseInt(rawMaterialIdStr, 10, 64)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	res, err := ac.RawMaterial.QueryRawMaterialInfo(rawMaterialId)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	return mvc.Response{
		Object : map[string]interface{}{"message": "成功", "data": res, "code":"200",},
	}
}

func (ac *AutoPurchaseController)PostAutommaticpurchaseinformationaudit() mvc.Result {

	ac.RawMaterial = new(model.RawMaterial)

	auditIdStr := ac.Ctx.FormValue("auditId")
	auditId, err := strconv.ParseInt(auditIdStr, 10, 64)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	result := ac.Ctx.FormValue("result")

	res, err := ac.RawMaterial.AutommaticPurchaseInformationAudit(auditId, result)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	return mvc.Response{
		Object : map[string]interface{}{"message": res["message"], "data": nil, "code":res["code"],},
	}
}

func (ac *AutoPurchaseController)PostAutommaticpurchaseinformationauditasync() mvc.Result {

	ac.RawMaterial = new(model.RawMaterial)

	auditIdStr := ac.Ctx.FormValue("auditId")
	auditId, err := strconv.ParseInt(auditIdStr, 10, 64)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	result := ac.Ctx.FormValue("result")

	res, err := ac.RawMaterial.AutommaticPurchaseInformationAuditAsync(auditId, result)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	return mvc.Response{
		Object : map[string]interface{}{"message": res["message"], "data": nil, "code":res["code"],},
	}
}

func (ac *AutoPurchaseController)PostQueryauditdetails() mvc.Result {

	ac.RawMaterial = new(model.RawMaterial)

	rawMaterialIdStr := ac.Ctx.FormValue("rawMaterialId")
	rawMaterialId, err := strconv.ParseInt(rawMaterialIdStr, 10, 64)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	res, err := ac.RawMaterial.QueryAuditDetails(rawMaterialId)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	return mvc.Response{
		Object : map[string]interface{}{"message": "成功", "data": res, "code":"200",},
	}
}

func (ac *AutoPurchaseController)PostGeneratepurchaseorder() mvc.Result {

	ac.RawMaterial = new(model.RawMaterial)

	automaticPurchaseDemandIdStr := ac.Ctx.FormValue("automaticPurchaseDemandId")
	automaticPurchaseDemandId, err := strconv.ParseInt(automaticPurchaseDemandIdStr, 10, 64)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	res, err := ac.RawMaterial.GeneratePurchaseOrder(automaticPurchaseDemandId)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	return mvc.Response{
		Object : map[string]interface{}{"message": res["message"], "data": nil, "code":res["code"],},
	}
}

func (ac *AutoPurchaseController)PostGenerateadvancepaymentdocument() mvc.Result {

	ac.RawMaterial = new(model.RawMaterial)

	purchaseOrderIdStr := ac.Ctx.FormValue("purchaseOrderId")
	purchaseOrderId, err := strconv.ParseInt(purchaseOrderIdStr, 10, 64)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	res, err := ac.RawMaterial.GenerateAdvancePaymentDocument(purchaseOrderId)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	return mvc.Response{
		Object : map[string]interface{}{"message": res["message"], "data": nil, "code":res["code"],},
	}
}

func (ac *AutoPurchaseController)PostGenerateexpenseapprovaldocument() mvc.Result {

	ac.RawMaterial = new(model.RawMaterial)

	prepaidApplicationFormIdStr := ac.Ctx.FormValue("prepaidApplicationFormId")
	prepaidApplicationFormId, err := strconv.ParseInt(prepaidApplicationFormIdStr, 10, 64)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	res, err := ac.RawMaterial.GenerateExpenseApprovalDocument(prepaidApplicationFormId)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	return mvc.Response{
		Object : map[string]interface{}{"message": res["message"], "data": nil, "code":res["code"],},
	}
}














